package com.ciwong.epaper.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.ViewTarget;
import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.me.bean.BookDeskCataloguesBean;
import com.ciwong.epaper.modules.me.bean.CatalogueUnitBean;
import com.ciwong.epaper.modules.me.bean.MaterialBean;
import com.ciwong.epaper.modules.me.bean.SubjectBean;

import java.util.List;


/**
 * Created by keepon on 2018/8/7.
 */
public class MaterialItemView extends RelativeLayout implements View.OnClickListener {
	TextView mTvSubjectName, mTvVersionName, mTvBookGrarde, mTvMoudle;
	LinearLayout mLlChangeMoudle, mLlChangeMaterial;
	RelativeLayout mRlStartStudy, mRlCatalogueFailed, mRlCatalogueSuccess;
	ImageView mIvSubject, mIvSubjectBg;
	private static final String TAG = "MaetrialItemView";
	private String mUnitBeanUnitid;
	private MaterialBean mSelectedMaterialBean;

	public MaterialItemView(Context context) {
		this(context, null);
	}

	public MaterialItemView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public MaterialItemView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		LayoutInflater.from(this.getContext()).inflate(R.layout.item_view_new_book_desk, this);
		init();
		initEvent();
	}

	private void initEvent() {
		mLlChangeMaterial.setOnClickListener(this);
		mLlChangeMoudle.setOnClickListener(this);
		mRlStartStudy.setOnClickListener(this);
		mRlCatalogueFailed.setOnClickListener(this);
	}

	private void init() {
		mTvSubjectName = findViewById(R.id.tv_subject_name);
		mIvSubject = findViewById(R.id.icon_subject);
		mIvSubjectBg = findViewById(R.id.iv_subject_bg);
		mTvMoudle = findViewById(R.id.tv_module);
		mTvVersionName = findViewById(R.id.tv_book_name);
		mTvBookGrarde = findViewById(R.id.tv_book_grade);
		mLlChangeMoudle = findViewById(R.id.ll_change_module);
		mLlChangeMaterial = findViewById(R.id.ll_change_material);
		mRlStartStudy = findViewById(R.id.rl_start_study);
		mRlCatalogueFailed = findViewById(R.id.rl_catalogue_failed);
		mRlCatalogueSuccess = findViewById(R.id.rl_catalogue_success);
		Log.e(TAG, "init: MaetrialItemView" );
	}

	public void bindData(SubjectBean subjectBean, BookDeskCataloguesBean.CataloguesBean cataloguesBean) {
		Log.e(TAG, "bindBookMarkData: MaetrialItemView" );
		if (subjectBean != null) {
			mTvSubjectName.setText(subjectBean.getSubjectName());
			setSubjectIcon(subjectBean);
			List<MaterialBean> materialBeanList = subjectBean.getMaterial();
			if (materialBeanList != null) {
				for (MaterialBean materialBean : materialBeanList) {
					if (materialBean.isSelected()) {
						mSelectedMaterialBean = materialBean;
						mTvVersionName.setText(materialBean.getVersionName());
						mTvBookGrarde.setText(getGradeBookName(materialBean));
					}
				}
			}
		}

		if (cataloguesBean != null) {

			List<CatalogueUnitBean> catalogueUnitBeanList = cataloguesBean.getData();
			if (catalogueUnitBeanList != null && catalogueUnitBeanList.size() > 0) {
				mRlCatalogueFailed.setVisibility(GONE);
				mRlCatalogueSuccess.setVisibility(VISIBLE);
				setTvModule(catalogueUnitBeanList);
			} else {
				mRlCatalogueSuccess.setVisibility(GONE);
				mRlCatalogueFailed.setVisibility(VISIBLE);
			}
		} else {
			mRlCatalogueFailed.setVisibility(VISIBLE);
			mRlCatalogueSuccess.setVisibility(GONE);

		}
	}

	private void setTvModule(List<CatalogueUnitBean> catalogueUnitBeanList) {
		for (CatalogueUnitBean catalogueUnitBean : catalogueUnitBeanList) {
			if (catalogueUnitBean.getChildren() != null && catalogueUnitBean.getChildren().size() > 0) {
				setTvModule(catalogueUnitBean.getChildren());
			} else {
				if (catalogueUnitBean.isSelected()) {
					mUnitBeanUnitid = catalogueUnitBean.getUnitid();
					mTvMoudle.setText(catalogueUnitBean.getName());
					return;
				}
			}
		}
	}


	private void setSubjectIcon(SubjectBean subjectBean) {
		Glide.with(getContext()).load(subjectBean.getSubjectIcon()).placeholder(R.mipmap.icon_subject).into(mIvSubject);

		Glide.with(getContext()).load(subjectBean.getSubjectBgMobile()).into(new ViewTarget<View, GlideDrawable>(mIvSubjectBg) {
			@Override
			public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
				mIvSubjectBg.setBackground(resource.getCurrent());
			}

			@Override
			public void onLoadFailed(Exception e, Drawable errorDrawable) {
				mIvSubjectBg.setBackgroundResource(R.mipmap.bg_book_desk_default);
			}
		});
	}

	public String getGradeBookName(MaterialBean materialBean) {
		String gradeName = materialBean.getGradeName();
		if (1201 == materialBean.getSemester()) {
			gradeName += "上册";
		} else if (1202 == materialBean.getSemester()) {
			gradeName += "下册";
		} else if (1203 == materialBean.getSemester()) {
			gradeName += "全册";
		}
		return gradeName;
	}


	@Override
	public void onClick(View v) {
		int id = v.getId();
		if (id == R.id.ll_change_material) {
			if (mOnMaterItemViewClicklistener != null) {
				mOnMaterItemViewClicklistener.onClickChangeMaterial(mSelectedMaterialBean);
			}
		} else if (id == R.id.ll_change_module) {
			if (mOnMaterItemViewClicklistener != null) {
				mOnMaterItemViewClicklistener.onClickChangeMoudle(mUnitBeanUnitid);
			}
		} else if (id == R.id.rl_start_study) {
			if (mOnMaterItemViewClicklistener != null) {
				mOnMaterItemViewClicklistener.onClickStartStudy();
			}
		} else if (id == R.id.rl_catalogue_failed) {
			if (mOnMaterItemViewClicklistener != null) {
				mOnMaterItemViewClicklistener.onClickRefreshCatalogue();
			}
		}
	}

	public void setOnMaterItemViewClicklistener(OnMaterItemViewClicklistener onMaterItemViewClicklistener) {
		mOnMaterItemViewClicklistener = onMaterItemViewClicklistener;
	}

	OnMaterItemViewClicklistener mOnMaterItemViewClicklistener;

	public interface OnMaterItemViewClicklistener {
		void onClickChangeMoudle(String unitBeanUnitid);

		void onClickChangeMaterial(MaterialBean selectedMaterialBean);

		void onClickStartStudy();

		void onClickRefreshCatalogue();
	}
}
